package ocr;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TensileZoom extends JFrame {
	private JPanel contentPane;
	private BorderLayout borderLayout1 = new BorderLayout();
	// 添加的组件
	private JPanel jpanel = new JPanel();
	private JButton jb1 = new JButton();
	private JSlider js1 = new JSlider();
	private JSlider js2 = new JSlider();
	private Border border1;
	private Border border2;
	private TitledBorder titleBorder1;
	private TitledBorder titleBorder2;
	private GridLayout gridLayout = new GridLayout();
	PanelDemo_2 pd2 = new PanelDemo_2();
	public void initial() {
		contentPane = (JPanel) this.getContentPane();
		border1 = BorderFactory.createEmptyBorder();
		border2 = BorderFactory.createEmptyBorder();
		titleBorder1 = new TitledBorder(border1, "水平方向的缩放");
		titleBorder2 = new TitledBorder(border2, "垂直方向的缩放");
		contentPane.setLayout(borderLayout1);
		this.setSize(new Dimension(600, 480));
		this.setTitle("图像的伸展和缩放演示");
		jb1.setFont(new Font("Dialog", Font.BOLD, 13));
		jb1.setText("重置");
		jb1.addActionListener(new ActionListener() {// 添加事件监听器
			public void actionPerformed(ActionEvent e) {
				jb1_actionPerformed(e);
			}
		});
		js1.setPaintLabels(true);
		js1.setBorder(titleBorder1);
		js1.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				js1_stateChanged(e);
			}
		});
		js2.setBorder(titleBorder2);
		js2.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				js2_stateChanged(e);
			}
		});
		gridLayout.setColumns(1);
		gridLayout.setRows(3);
		contentPane.add(jpanel, BorderLayout.SOUTH);
		jpanel.add(js1, null);
		jpanel.add(js2, null);
		jpanel.add(jb1, null);
		// 在窗口中部添加PanelDemo_2面板
		contentPane.add(pd2, BorderLayout.CENTER);
		this.setVisible(true);
	}
	public void jb1_actionPerformed(ActionEvent e) {
		pd2.reset();
		pd2.repaint();
	}
	public void js1_stateChanged(ChangeEvent e) {
		// 设置新的X轴上的缩放比例，变换图像，并绘制相应的图像
		double scafex = ((double) ((JSlider) e.getSource()).getValue()) / 100;
		System.out.println("X" + " " + scafex);
		pd2.scafeX = scafex;
		pd2.applyFilter();
		pd2.repaint();
	}
	public void js2_stateChanged(ChangeEvent e) {
		// 设置新的Y轴上的缩放比例，变换图像，并绘制相应的图像
		double scafey = ((double) ((JSlider) e.getSource()).getValue()) / 100;
		System.out.println("Y" + " " + scafey);
		pd2.scafeY = scafey;
		pd2.applyFilter();
		pd2.repaint();
	}
	public static void main(String[] args) {
		new TensileZoom().initial();
	}
}
class PanelDemo_2 extends JPanel {
	private BorderLayout borderLayout = new BorderLayout();
	// 定义新属性
	Image image;// 源图像
	BufferedImage bi1;// 缓冲区图像
	BufferedImage bi2;// 缓冲区图像
	BufferedImage bi3;// 缓冲区图像
	Graphics2D g2d;// 图像上下文
	AffineTransform transform = new AffineTransform();// 一个仿射变换过滤器
	double scafeX = 1.0;// x轴上的缩放比例
	double scafeY = 1.0;// Y轴上的缩放比例
	public PanelDemo_2() {
		this.setLayout(borderLayout);
		loadImage();// 加载图片
		createBufferedImage();// 创建缓冲区图像
		bi1 = bi2;
		this
				.setSize(new Dimension(image.getWidth(this), image
						.getHeight(this)));// 设置面板大小
	}
	public void loadImage() {
		// 加载图片
		image = this.getToolkit().getImage("C:\\Users\\啊林\\Desktop\\图片检测\\4.jpg");
				//这里如果不填写一个存在的图片，程序会抛出空指针异常
//				ClassLoader.getSystemResource("java.jpg"));// 创建Image对象
		MediaTracker mt = new MediaTracker(this);
		mt.addImage(image, 0);
		try {
			mt.waitForAll();
		} catch (Exception e) {
			e.printStackTrace();// 表示加载失败
		}
		if (image.getWidth(this) == -1) {
			System.out.println("不能创建图象");// 如果不存在退出程序
			System.exit(1);
		}
	}
	public void createBufferedImage() {
		// 根据image对象创建一个缓冲区图像
		bi2 = new BufferedImage(image.getWidth(this), image.getHeight(this),
				BufferedImage.TYPE_INT_ARGB);
		// 向缓冲区图像中输入原image对象的图象数据
		g2d = bi2.createGraphics();
		g2d.drawImage(this.image, 0, 0, this);// 绘制缓冲区图像
		// 创建另一个空的缓冲区图像，大小和类型相同的bi3
		bi3 = new BufferedImage(image.getWidth(this), image.getHeight(this),
				BufferedImage.TYPE_INT_ARGB);
	}
	public void reset() {
		g2d.setColor(Color.black);
		g2d.clearRect(0, 0, image.getWidth(this), image.getHeight(this));
		g2d.drawImage(this.image, 0, 0, this);// 绘制缓冲区图像
		// 令bi1对象指向源图像存储的bi2缓冲区图像
		bi1 = bi2;
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(bi1, 0, 0, this);
	}
	public void update(Graphics g) {
		g.clearRect(0, 0, this.getWidth(), this.getHeight());
		this.paintComponent(g);
	}
	public void applyFilter() {
		transform.setToScale(this.scafeX, this.scafeY);// 设置变换在x、y轴上的缩放比例
		// 创建一个仿射变换过滤器
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		// 清空bi3缓冲区图像中的数据
		bi3.createGraphics().clearRect(0, 0, bi3.getWidth(this),
				bi3.getHeight(this));
		// 利用过滤器塓滤源图像，并将得到的处理后图像数据存入bi3中
		ato.filter(bi2, bi3);
		bi1 = bi3;// 令bi1指缶bi3对象，以便显示bi3中存储的图像数据
	}
}
